<template>
  <el-dialog title="授权修改" :visible.sync="dialogShow">
    <el-form label-width="120px">
      <el-form-item label="授权名">
        <el-input v-model="agent_name" disabled />
      </el-form-item>
      <el-form-item label="授权品牌">
        <el-input v-model="brand_name" disabled />
      </el-form-item>
      <el-form-item label="当前等级">
        <el-input v-model="level_name" disabled />
      </el-form-item>
      <el-form-item label="新授权等级">
        <el-col :span="9">
          <el-select v-model="level_int" @change="selectLevel">
            <el-option
              v-for="item in levels"
              :key="item.id"
              :label="item.level_name"
              :value="item.level_int"
            >
              <span style="float: left;color: #8492a6;">{{ item.level_int }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.level_name }}</span>
            </el-option>
          </el-select>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitBtn">提交</el-button>
        <el-button @click="dialogShow = false">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { operationAgentUpAndDownLevelApi, uploadWarrant } from '@/api/agent'
import { conversion } from '@/utils/rules'
import { drawPhoto } from '@/utils/drawPhoto'
export default {
  data() {
    return {
      dialogShow: false,
      brand_name: '',
      level_name: '',
      levels: [],
      level_int: '',
      level_name_new: '',
      agent_name: '',
      brand_id: '',
      anent_id: '',
      people_card_num: '',
      anent_login_name: '',
      code_num: '',
      yes_date: ''
    }
  },
  methods: {
    submitBtn() {
      this.currentLevel(this.level_int)
      this.$confirm(`此操作将授权名为[ ${this.agent_name} ]的授权等级修改为[ ${this.level_name_new} ], 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const data = {
          anent_id: this.anent_id,
          brand_id: this.brand_id,
          level_int: this.level_int
        }
        console.log(data)
        operationAgentUpAndDownLevelApi(data).then(res => {
          console.log(res)
          if (res.code === 200) {
            const textArr = [
              {
                text: this.agent_name,
                x: 270,
                y: 420
              },
              {
                text: conversion(this.people_card_num, 6, 14, 8),
                x: 270,
                y: 475
              },
              {
                text: this.anent_login_name,
                x: 270,
                y: 530
              },
              {
                text: this.code_num,
                x: 270,
                y: 585
              },
              {
                text: res.brand_level.level_name,
                x: 270,
                y: 640
              },
              {
                text: this.yes_date,
                x: 270,
                y: 695
              }
            ]
            let warrantImg = ''
            console.log(textArr, warrantImg)
            drawPhoto(res.brand_content.approval_img, { width: '800', height: '1131' }, textArr, imgUrl => {
              console.log(imgUrl)
              warrantImg = imgUrl
              const that = this
              const imgData = {
                anent_id: that.anent_id,
                brand_id: that.brand_id,
                code_num: that.code_num,
                anent_approval_img: warrantImg
              }
              console.log(imgData)
              uploadWarrant(imgData).then(res => {
                console.log(res)
                if (res.code === 200) {
                  this.$message.success('授权等级修改成功！')
                  this.dialogShow = false
                  this.level_int = ''
                  this.$emit('reload')
                }
              })
            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    selectLevel(val) {
      console.log(val, this.level_int)
    },
    currentLevel(level_int) {
      this.levels.forEach(item => {
        if (item.level_int === level_int) {
          this.level_name_new = item.level_name
          return false
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="sass" scoped>

</style>
